<?php

include_once(dirname(__FILE__).'/../services.database.php');

class DBSysUser extends DataService {

	public function getInstance(){	
		return(new DBSysUser());
	}

	public function logUserAction($action){
	
		$log_data = array(
			'id_usr_sul' => $_SESSION['id_usr'],
			'remote_address_sul' => $_SERVER['REMOTE_ADDR'],
			'action_sul' => $action,
		);
	
		$this->executeInsert('sys_user_log_sul',$log_data);
	}
	
	public function getUser($username_usr){
		
		$sql = "SELECT *
				  FROM sys_user_usr
				 WHERE username_usr='%s'";
	
		return $this->SafeGetRow($sql,$username_usr);
		
	}
	
	public function getClient($id_usr){
		
		$sql = "SELECT *
				  FROM rcd_client_clt
				  WHERE id_usr_clt = %i
			   ";
		
		return $this->SafeGetRow($sql,$username_usr);
	
	}
	
	
	
/*
	public function getSharedTipos(){
		return($this->db->GetAll("select * from shared_tipos order by categoria,descripcion"));
	}
	
	public function getTipo($categoria){
		return($this->db->GetAll("select * from shared_tipos where categoria='$categoria' order by descripcion"));
	}

	protected function getPrimarySequence($columns){
		foreach($columns as $c){
			if($c->primary_key){
				$this->log->debug($c->default_value);
				if(preg_match('/.*_seq.*$/',$c->default_value)){
					return($c->name);
				}	
			}
		}
		return(null);
	}
	
	public function isTableSerial($tableName){
		$columns = $this->db->MetaColumns($tableName,true);
		return($this->getPrimarySequence($columns));
	}
	
		
	protected function getColumn($columns,$columName){
		foreach($columns as $c)
			//fijarse un string equals insensitive
			if($c->name == $columName) 
				return($c);
	}
	//cambiar para que use los tipos y claves de fields
	protected function getAutoSelect($tableName,$params){
		$columns = $this->db->MetaColumns($tableName,true);
		$sql = "select * from ${tableName} where 1=1";
		$values = array();
		
		//hay que obtener el tipo de las columnas.
		foreach($params as $key => $value ){
			$col = $this->getColumn($columns,$key);
			$sql .= " and ${key}=" . $this->getQuote($col->type);
			$values[] = $value;
		}
		$sql = $this->sqlParser->query($sql,$values);
		$this->log->debug($sql);
		return($sql);
	}
		
	public function getRegister($tableName,$loadFields=array()){
		$sql = $this->getAutoSelect($tableName,$loadFields);
		return($this->db->GetRow($sql));	
	}
	
	public function getQuote($type){
		$this->log->debug("Type Column:" . $type);
		switch($type){
			case 'int4' : return('%i');
			default : return("'%s'");
		}
	}
	
	public function updateRegister($tableName,$params){
		$columns = $this->db->MetaColumns($tableName,true);
		$condition = " 1=1 ";
		$values = array();

		foreach($columns as $c){
			$this->log->debug("Name:" . $c->name);
			$this->log->debug("Type:" . $c->type);
			$this->log->debug("Meta Type:" . $this->db->MetaType($c->type));
			$this->log->debug("Value:" . $params[$c->name]);
			$this->log->debug("");
						
			if($c->primary_key){
				$pk_name = $c->name;
                $condition .= " and " . $c->name . "=" . $this->getQuote($c->type);
				$values[] = $params[$c->name];
			}
		}

		$this->log->debug("CONDITION:" . $condition);
			$condition = $this->sqlParser->query($condition,$values);
			$query = "select * from ${tableName} where ${condition}";				
		
		$this->log->debug("FINAL QUERY:" . $query);
		$this->log->debug("SERIAL:" . $this->getPrimarySequence($columns));
				
		if($this->db->getRow($query)){
			$this->log->debug('function executeUpdate');
			return(array($pk_name => $this->executeUpdate($tableName,$params,$condition)));
		} else {
			$this->log->debug('function executeInsert');
			return(array($pk_name => $this->executeInsert($tableName, $params, $this->getPrimarySequence($columns))));
		}	
	}
	
	public function deleteRegister($tableName,$id){
		$columns = $this->db->MetaColumns($tableName,true);
		$condition = " 1=1 ";
		$values = array();
		
		foreach($columns as $c){
			$this->log->debug($c->name);
			$this->log->debug("Type:" . $c->type);
			$this->log->debug("Meta Type:" . $this->db->MetaType($c->type));
			$this->log->debug("Value:" . $params[$c->name]);
						
			if($c->primary_key){
				$condition .= " and " . $c->name . "=" . $id;
				$values[] = $params[$c->name];
			}
		}

		$this->log->debug("FINAL QUERY:" . $query);
		$this->log->debug('Delete: delete from $tableName where $condition');
		return($this->db->Execute("delete from $tableName where $condition"));
				
	}
	
	public function getRowsFromTable($table, $start, $limit, $sort, $dir, $id_tramite, $filter_category = null, $filter_value = null, $filter_nro_category = 0){
	
		$sql = "select *,'${table}' as ttable from $table where";
		
		for($i=1; $filter_category != null, $i<=$filter_nro_category ; $i++){
			$sql .= " and ".$filter_category->$i." = '".$filter_value->$i."'";		
		}
		
		$sql = str_replace('where and','where',$sql);
		
		return($this->GetAll($sql, $limit, $start, $sort, $dir));
		
	}
*/
}

?>
